function custom_profile = sine_wave(base_temp, max_temp, period, num_of_waves)
% SINE_WAVE creates a custom profile in TCS format of a semi-sine wave,
% repeated multiple amount of times

% base_temp and max_temp in °C or in 1/10 °C
% period in seconds

%% Data Amount
Segments = 20 ; % Number of segments to send to the TCS
% Reduce this number to make a less detailed but faster-to-send profile.
%% Temperature format
if base_temp>99
    base_temp=base_temp/10;
end

if max_temp>99
    max_temp=max_temp/10;
end
%% The Profile
Wave = round((sin(pi * (1:Segments) / Segments) * (max_temp - base_temp) + base_temp) * 10) ;     % Generates the temperature commands for one wave 
Time_Bin_Length = round(100 * period / length(Wave)) ;                                                                         % The duration of each segment in 10ms
Total_Segment_Number = length(Wave)* num_of_waves ;                                                                                    % The number of segments in total
if Total_Segment_Number > 999   % The TCS can only handle up to 999 segments in its 'Uw...' command.
    error('Warning : There are too many segments in the custom profile for the TCS to handle, please reduce the variable ''Segments'' in the ''sine_wave'' function.') ;
end
Wave_Data = zeros(1, 2*length(Wave));               % Pre-allocation for speed interests
Wave_Data(1:2:end)= Time_Bin_Length ;               % every odd triplet is the duration of the segment in 10 of milliseconds
for i=1:length(Wave)                                % every even triplet is the temperature command in 10th of °C
    Wave_Data(2*i) = Wave(i);                           % For more details, see 'TCS_Commands.txt'
end
custom_profile = ['Uw11111' sprintf('%03d',Total_Segment_Number) repmat(sprintf('%03d', Wave_Data(:)), 1, num_of_waves)];
end

